package order.servlet;

import com.alibaba.fastjson.JSON;
import order.entity.Orderbiao;
import order.service.OrderbiaoService;
import order.service.impl.OrderbiaoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/ListServlet")
public class ListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String pageStr=request.getParameter("page");
        String pagesizeStr=request.getParameter("pageSize");

        int page=pageStr!=null? Integer.parseInt(pageStr):1;
        int pageSize=pagesizeStr!=null? Integer.parseInt(pagesizeStr):5;

        OrderbiaoService service=new OrderbiaoServiceImpl();
        List<Orderbiao> resultList=service.getList(page,pageSize);
        int count=service.getCount();//总记录数
        int pageCount=count% pageSize==0?count/pageSize:(count/pageSize)+1;


        Map<Object,Object> pageMap = new HashMap<>();

        pageMap.put("resultList",resultList);
        pageMap.put("pageCount",pageCount);//总记录数
        pageMap.put("page",page);//页数
        pageMap.put("pageSize",pageSize);

        PrintWriter out=response.getWriter();
        out.print(JSON.toJSONString(pageMap));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
